package com.system.service.impl;

import java.util.List;

import com.base.service.impl.ServiceImpl;
import com.system.pojo.Person;
import com.system.service.IPersonService;
import com.system.struts2.util.MD5Util;

public class PersonServiceImpl<T extends Person> extends ServiceImpl<T>
		implements IPersonService<T> {

	public T findPersonByAccount(String account) {

		List<T> person = this
				.getDao()
				.createQuery(
						" select p from Person p "
								+ " where lower(p.account) = lower('"+account.trim()+"') and deleted = false ").list();

		if (person.size() > 0)
			return person.get(0);

		return null;
	}

	@Override
	@SuppressWarnings("unchecked")
	public boolean create(T person) {

		if (findPersonByAccount(person.getAccount()) != null) {

			return false;
		} else {

			person.setPassword(MD5Util.calc(person.getPassword()));

			this.getDao().create(person);
			return true;
		}
	}

	public T getPerson(String account, String password) {

		List<T> list = this
				.getDao()
				.createQuery(
						" select p from Person p where p.account = '" + account
								+ "' and p.password = '"
								+ MD5Util.calc(password)
								+ " ' and p.deleted = false ").list();

		if (list.size() > 0)
			return list.get(0);
		return null;
	}

}
